<template>
  <div class="login" v-if="isCode">
    <!-- <img class="logoStyle" src="../../assets/images/login-logo.png" alt /> -->
    <div class="logoStyle">数据集成系统</div>
    <div class="header">
      <!-- <span class="header-tit">前端框架标准版</span> -->
      <span class="header-login">欢迎您，登录系统!</span>
    </div>

    <div class="content">
      <el-row :gutter="20">
        <el-col :span="6" :offset="5">
          <div class="fl-box">
            <div class="fl-bg"></div>
          </div>
        </el-col>
        <el-col :span="6" :offset="3">
          <div class="fr-box">
            <div class="fr-box-content">
              <div class="login-tit">
                <p>
                  登录
                  <span>Sign in</span>
                </p>
              </div>
              <div class="login-content">
                <el-form :model="loginForm" ref="loginForm" :rules="rules">
                  <el-form-item prop="username">
                    <el-input placeholder="用户名" v-model.trim="loginForm.username" @focus="inputFocus" name="name">
                      <i slot="prefix" class="el-input__icon person"></i>
                    </el-input>
                  </el-form-item>
                  <el-form-item prop="password" :class="{ marginToggle: error }">
                    <el-input type="password" placeholder="密 码" v-model.trim="loginForm.password" @keyup.enter.native="loginBtn" @focus="inputFocus" name="passwd">
                      <i slot="prefix" class="el-input__icon password-icon"></i>
                    </el-input>
                    <p class="error" v-if="error">用户名和密码不正确</p>
                  </el-form-item>
                  <el-row :gutter="20">
                    <el-col :span="12">
                      <el-input v-model.trim="loginForm.captcha" placeholder="请输入验证码" @keyup.enter.native="loginBtn"></el-input>
                    </el-col>
                    <el-col :span="12">
                      <div class="verify-img">
                        <img :src="src" @click="changeImg" />
                      </div>
                    </el-col>
                  </el-row>
                  <div class="rember-pass">
                    <el-checkbox v-model.trim="checked">记住密码</el-checkbox>
                  </div>
                  <el-button type="primary" @click="loginBtn">登录</el-button>
                </el-form>
              </div>
            </div>
          </div>
        </el-col>
      </el-row>
    </div>

    <div class="footer">
      <!-- <p>whb</p> -->
    </div>
    <el-dialog title="授权登录" :visible.sync="dialogFormVisible" width="800px" :close-on-press-escape="false" :close-on-click-modal="false">
      <el-form ref="sqForm" :rules="rules2" :model="sqForm" label-width="80px">
        <el-row type="flex" justify="space-between">
          <el-col :span="22">
            <el-form-item label="申请码" disabled>
              <el-input type="textarea" :rows="3" v-model="sqForm.jqm" placeholder="申请码" disabled></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row type="flex" justify="space-between">
          <el-col :span="22">
            <el-form-item label="授权码" prop="sqm">
              <el-input v-model="sqForm.sqm" type="textarea" :rows="2" placeholder="授权码"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="dialogFormVisible = false">取 消</el-button>
        <el-button size="small" type="primary" @click="submitSq">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
// import { getImg } from '@/api/login'
import { isShouquan, Shouquan } from "@/api/login";
let Base64 = require("js-base64").Base64;
export default {
  data() {
    return {
      dialogFormVisible: false,
      isCode: true,
      checked: false,
      sqForm: {
        jqm: ""
      },
      baseUrl: process.env.VUE_APP_BASE_URL,
      src:
        process.env.VUE_APP_BASE_URL +
        "/login/captcha.jpg?" +
        new Date().getTime(),
      loginForm: {
        username: "",
        password: "",
        captcha: ""
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" }
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }]
      },
      rules2: {
        sqm: [{ required: true, message: "不能为空", trigger: "blur" }]
      }
    };
  },
  methods: {
    submitSq() {
      var _this = this;
      this.$refs.sqForm.validate((valid) => {
        if (valid) {
          Shouquan(_this.sqForm.sqm).then((res) => {
            var data = res.data;
            if (data.success) {
              _this.$message({
                type: "success",
                message: "授权成功，请重新登录"
              });
              _this.dialogFormVisible = false;
            } else {
              _this.$message({
                type: "error",
                message: data.msg + "!  请重试"
              });
            }
          });
        }
      });
    },
    loginBtn() {
      let _this = this;
      isShouquan().then((res) => {
        var data = res.data;
        if (!data.success) {
          _this.$message({
            type: "error",
            message: data.msg
          });
          _this.sqForm.jqm = data.license;
          clearTimeout(_this.timer);
          _this.timer = setTimeout(function () {
            _this.dialogFormVisible = true;
          }, 1000);
        } else {
          if (this.checked) {
            this.setCookie(
              this.loginForm.username,
              Base64.encode(this.loginForm.password),
              7
            );
          } else {
            this.clearCookie();
          }
          this.$refs.loginForm.validate((valid) => {
            if (valid) {
              _this.$store
                .dispatch("loginSecurity", _this.loginForm)
                .then((res) => {
                  // this.loginToken();
                  //   _this.$store
                  // .dispatch("GetUserInfo", _this.loginForm)
                  _this.$router.push({ path: "/home" });
                })
                .catch(() => {
                  this.$message({
                    type: "error",
                    message: "登录失败"
                  });
                });
            }
          });
        }
      });
    },
    inputFocus() {
      this.$store.state.user.error = false;
    },
    changeImg() {
      this.src = "";
      this.$nextTick(() => {
        this.src =
          this.baseUrl + "/login/captcha.jpg" + "?" + new Date().getTime();
      });
    },
    getCookie() {
      if (document.cookie.length) {
        let arr = document.cookie.split("; ");
        for (var i = 0; i < arr.length; i++) {
          var arr2 = arr[i].split("=");
          if (arr2[0] === "userName") {
            this.loginForm.username = arr2[1];
          } else if (arr2[0] === "userPwd") {
            this.loginForm.password = Base64.decode(arr2[1]);
          }
        }
      }
    },
    // eslint-disable-next-line camelcase
    setCookie(c_name, c_pwd, exdays) {
      var exdate = new Date();
      exdate.setTime(exdate.getTime() + 24 * 60 * 60 * 1000 * exdays);
      window.document.cookie =
        // eslint-disable-next-line camelcase
        "userName" + "=" + c_name + ";path=/;expires=" + exdate.toGMTString();
      window.document.cookie =
        // eslint-disable-next-line camelcase
        "userPwd" + "=" + c_pwd + ";path=/;expires=" + exdate.toGMTString();
    },
    clearCookie: function () {
      this.setCookie("", "", -1);
    },
    windowLocationSearch() {
      let search = window.location.search.split("?");
      if (search.length === 2) {
        let obj = {};
        let arr = search[1].split("&");
        // 统一登录-code登录
        if (arr.some((a) => a.split("=")[0] === "code")) {
          obj.code = arr
            .filter((a) => a.split("=")[0] === "code")[0]
            .split("=")[1];
          this.$store
            .dispatch("loginSecurity", { username: obj.code })
            .then((res) => {
              this.$router.push({ path: "/" });
            })
            .catch(() => {
              // this.$message.error('认证失败')
              // this.$router.push({ path: '/login' })
            });
        }
        if (arr.some((a) => a.split("=")[0] === "name")) {
          obj.code = arr
            .filter((a) => a.split("=")[0] === "name")[0]
            .split("=")[1];
          this.$store
            .dispatch("loginSecurity", { username: obj.code })
            .then((res) => {
              this.$router.push({ path: "/" });
            })
            .catch(() => {
              // this.$message.error('认证失败')
              // this.$router.push({ path: '/login' })
            });
        }
      }
    }
  },
  mounted() {
    // this.windowLocationSearch()
    this.getCookie();
  },
  computed: {
    error() {
      return this.$store.getters.error;
    }
  }
};
</script>


<style lang="scss" scoped>
.login {
  height: 100vh;
  min-width: 1000px;
  background: url("../../assets/images/login.png") no-repeat center;
  background-size: 100% 100%;
  position: relative;
  .logoStyle {
    position: absolute;
    top: 42vh;
    left: 11vw;
    width: 36vw;
    font-size: 40px;
    // color: #fff;
  }
  .header {
    height: 120px;
    padding: 0 30px;
    span {
      line-height: 120px;
    }
    .header-tit {
      color: #3383d0;
      font-size: 34px;
      font-family: "黑体";
      font-weight: bold;
    }
    .header-login {
      float: right;
      color: #fff;
      font-size: 14px;
    }
  }
  .content {
    background-size: 100%;
    width: 100%;
    height: calc(100% - 240px);
  }
  // .footer {
  //   height: 120px;
  //   line-height: 120px;
  //   text-align: center;
  //   p {
  //     color: #999999;
  //     font-size: 14px;
  //     margin: 0;
  //   }
  // }
  .el-row,
  .fl-box,
  .fr-box {
    height: 100%;
    width: 100%;
  }
  .el-col {
    height: 100%;
  }
  .fr-box,
  .fl-box {
    display: flex;
    justify-content: center;
    // align-items: center;
    padding-top: 9vh;
  }
  .fl-bg {
    height: 100%;
    width: 340px;
    img {
      height: 100%;
    }
  }
  .fr-box-content {
    background: rgba(255, 255, 255, 0.3);
    box-shadow: 5px 9px 24px 0px rgba(6, 58, 111, 0.3);
    border-radius: 6px;
    width: 22vw;
    height: 45vh;
    padding: 0 35px 20px 35px;
    .login-tit {
      p {
        color: #135d95;
        /* text-align: center; */
        /* line-height: 89px; */
        margin: 2.6vh 0 3vh 0;
        font-size: 1.5vw;
        span {
          color: #333333;
          font-size: 1vw;
        }
      }
      i {
        width: 100%;
        height: 20px;
        background: url("../../assets/images/login_lucency.png") no-repeat;
        display: inline-block;
      }
    }
    .login-content {
      .person {
        background: url("../../assets/images/username.png") no-repeat;
        width: 13px;
        height: 13px;
      }
      .password-icon {
        background: url("../../assets/images/password.png") no-repeat;
        width: 13px;
        height: 13px;
      }
      .error {
        font-size: 14px;
        color: red;
      }
      .marginToggle {
        margin-bottom: 0;
      }
    }
    .el-row,
    .el-col {
      height: 5vh;
    }
    .verify-img {
      height: 100%;
      margin-right: -20px;
      img {
        height: 100%;
      }
    }
    .rember-pass {
      margin: 20px 0;
      span {
        color: #999999;
        font-size: 14px;
      }
    }
  }
  img {
    display: inline-block;
    height: auto;
    max-width: 100%;
  }
}
</style>
<style lang="scss">
@import "../../assets/style/style.scss";
.login {
  .el-input__prefix {
    display: flex;
    align-items: center;
  }
  .rember-pass {
    span {
      color: #000;
      font-size: 14px;
    }
  }
  // .el-button {
  //   width: 100%;
  // }
  .el-form-item {
    margin-bottom: 2vh;
  }
  .el-form-item--medium .el-form-item__content {
    line-height: 6vh;
  }
  .el-input--medium .el-input__inner {
    height: 5vh;
    line-height: 6vh;
  }
  .el-button--medium {
    padding: 1.5vh 1vw;
    // margin-top: 3vh;
    width: 12vw;
    display: block;
    margin: 0 auto;
    margin-top: 3vh;
    background: linear-gradient(
      90deg,
      rgba(19, 92, 148, 1),
      rgba(30, 134, 200, 1)
    );
    border-radius: 5px;
    border: none;
    font-size: 1vw;
  }
}
</style>
